//
//  EFengChatBaseView.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/2/22.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengChatBaseView.h"
#import "ChatMessageListCell.h"
#import "ChatMessageListCellData.h"
#import "CMessageMgr.h"
@interface EFengChatBaseView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,assign)NSInteger unreadCount;
@end

@implementation EFengChatBaseView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.backgroundColor = ClearColor;
        self.isNetconnect = YES;
    }
    return self;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = ClearColor;
        _tableView.scrollEnabled = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self addSubview:_tableView];
    }
    return _tableView;
}


- (void)setMessages:(NSArray *)messages
{
    _messages = messages;
    [self.tableView reloadData];
    self.unreadCount = [[[CMessageMgr alloc]init] loadAllUnReadCount];

    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    if (section == 1)
    NSInteger count = 0;
    @synchronized(self){
        count = self.messages.count;
    }
    return count;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    CGFloat height = 0;
    if (!self.isNetconnect) {
        height=35;
    }else{
        height = 0.01f;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0.01f;
    return height;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    if (!self.isNetconnect) {
        
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, HARDWARE_SCREEN_WIDTH, 35)];
        UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,14, 14)];
        leftImage.image = [UIImage imageNamed:@"loginErrorImage"];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(34, 0, HARDWARE_SCREEN_WIDTH-80, 35)];
        lable.text = self.serError;
        [lable setFont:Font14];
        lable.textAlignment = UITextAlignmentCenter;
        UIImageView *rightimage = [[UIImageView alloc]initWithFrame:CGRectMake(HARDWARE_SCREEN_WIDTH-50 - 6,0,50,35)];
        rightimage.image=[UIImage imageNamed:@"rightArrow"];
        [headerView addSubview:leftImage];
        [headerView addSubview:lable];
        [headerView addSubview:rightimage];
        headerView.backgroundColor=RGBCOLOR(247, 236, 157);
        UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, 35)];
        [control addTarget:self action:@selector(sectionDidClick) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:control];
        
        
        
        return headerView;
    }else{
        return nil;
    }

}

- (void)sectionDidClick
{
    if ([self.eFengChatBaseViewDelegate respondsToSelector:@selector(EFengChatBaseViewNetSectionDidClick)] && self.eFengChatBaseViewDelegate != nil) {
        [self.eFengChatBaseViewDelegate EFengChatBaseViewNetSectionDidClick];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    return nil;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier=@"messageCell";
    ChatMessageListCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell=[[ChatMessageListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier andWidth:CGRectGetWidth(tableView.frame)];
    }
    
    if ([tableView numberOfRowsInSection:0] > indexPath.row + 1) {
        cell.shortLine.hidden = NO;
        cell.longLine.hidden = YES;
    } else {
        cell.shortLine.hidden = YES;
        cell.longLine.hidden = NO;
    }
    
    [cell setInfomation:[self.messages objectAtIndex:indexPath.row]];
//    @synchronized(self){
//        if(self.messages.count >indexPath.row){
//        } else {
//            
//        }
//    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//   [[NSOperationQueue mainQueue] addOperationWithBlock:^{
//       
//   }];
    if ([self.eFengChatBaseViewDelegate respondsToSelector:@selector(EFengChatBaseViewDidSelectWithMessage:)] && self.eFengChatBaseViewDelegate != nil) {
        ChatMessageListCellData *data = self.messages[indexPath.row];
        [self.eFengChatBaseViewDelegate EFengChatBaseViewDidSelectWithMessage:data];
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        return 70.0f;
    }
    return 60.0f;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


//// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_tableView == tableView) {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            if ([self.eFengChatBaseViewDelegate respondsToSelector:@selector(EFengChatBaseViewDidCommitEditWithMessage:)] && self.eFengChatBaseViewDelegate != nil) {
                
                ChatMessageListCellData *data = [self.messages objectAtIndex:indexPath.row];
                [self.eFengChatBaseViewDelegate EFengChatBaseViewDidCommitEditWithMessage:data];
            }
   
        }
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
        
    } else {
        
        
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.tableView.frame = self.bounds;
}

@end
